<template>
    <div>
      <div>
        <span style="font-weight: lighter;">查询用户名：</span>
        <el-input style="width: 200px;" placeholder="查询用户名" v-model="username"></el-input>
        <span style="font-weight: lighter;">查询姓名：</span>
        <el-input style="width: 200px; margin: 0 8px; " placeholder="查询姓名" v-model="name"></el-input>
        <el-button type="primary" @click="load(1)">查询</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </div>
      <div style="margin: 10px 0;">
        <el-button type="primary" plain @click="handleAdd">新增管理员</el-button>
        <el-button type="danger" plain @click="delBatch">批量删除</el-button>
      </div>
      <el-table :data="users" stripe :header-cell-style="{ backgroundColor:'aliceblue' , color: '#333'}"  @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
        <el-table-column label="姓名" prop="name" align="center"></el-table-column>
        <el-table-column label="手机号" prop="phone" align="center"></el-table-column>
        <el-table-column label="用户名" prop="username" align="center"></el-table-column>
        <el-table-column label="密码" prop="password" align="center"></el-table-column>
        <el-table-column label="头像" align="center">
          <template v-slot="scope">
            <div>
              <el-image style="width: 50px; border-radius: 50%; " v-if="scope.row.image" :src="scope.row.image"
                :preview-src-list="[scope.row.image]"></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 10px 0;">
        <el-pagination background layout="total,sizes, prev, pager, next" :total="total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" :current-page.sync="currentPage2" :page-sizes="[5, 10, 20]"
          :page-size="pageSize">
        </el-pagination>
      </div>
      <div>
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" >
          <el-form :model="form" label-width="80px" style="padding-right: 20px;" :rules="rules" ref="formRef">
            <el-form-Item label="用户名" prop="username">
              <el-Input v-model="form.username" placeholder="用户名"></el-Input>
            </el-form-Item>
            <el-form-Item label="姓名" prop="name">
              <el-Input v-model="form.name" placeholder="姓名"></el-Input>
            </el-form-Item>
            <el-form-Item label="电话" prop="phone">
              <el-Input v-model="form.phone" placeholder="电话"></el-Input>
            </el-form-Item>
            <el-form-Item label="邮箱" prop="email">
              <el-Input v-model="form.email" placeholder="邮箱"></el-Input>
            </el-form-Item>
            <el-form-Item label="地址" prop="address">
              <el-Input type="textarea" v-model="form.address" placeholder="地址"></el-Input>
            </el-form-Item>
            <el-form-Item label="头像">
              <el-upload class="avatar-uploader" :action="$baseUrl" :headers="{token:user.token}"
                file-list="form.image? [form.image]:[]" list-type="picture"  :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <el-button type="primary">上传头像</el-button>
              </el-upload>
            </el-form-Item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </template>
  <script>
  import request from "@/utils/request"
  export default {
     name: 'UserView',
     data() {
       return {
          users:[],
          pageSize:10,
          pageNum:1,
          username:'',
          name:'',
          total:'',
          dialogVisible: false,
          form:{},
          user:JSON.parse(localStorage.getItem('user'||'{}')),
          rules:{
            username:[
              {Required:true,message:'请输入账号',trigger:'blur'},
            ]
          },
          ids:[]
       }
     },
     created(){
      this.load()
     },
  
     mounted() {
      
     },
     methods: {
      indexMethod(index) {
       return (this.pageNum - 1) * this.pageSize + index + 1;
     },
      delBatch(){
        if(!this.ids.length){
          this.$message.warning('请选择数据')
          return
        }
        this.$confirm('您确认删除吗','确认',{type:"warning"}).then(response=>{
          this.$request.delete('/user/delete',{data:this.ids}).then(res=>{
            console.log(response)
            if(res.code=='1'){
              this.$message.success('操作成功')
              this.load(1)
              localStorage.setItem('user',JSON.stringify(this.user))
              this.$emit('update:user',this.user)
            }else{
              this.$message.error(res.msg)
            }
          })
     
        }).catch(()=>{})
      },
      handleSelectionChange(rows){
        this.ids=rows.map(v=>v.id)
      },
      del(id){
        this.$confirm('您确认删除吗','确认',{type:"warning"}).then(response=>{
          this.$request.delete('/user/delete/'+id).then(res=>{
            console.log(response)
            if(res.code=='1'){
              this.$message.success('操作成功')
              this.load(1)
            }else{
              this.$message.error(res.msg)
            }
          })
     
        }).catch(()=>{})
      },
      handleEdit(row){
        this.dialogVisible=true
        this.form=JSON.parse(JSON.stringify(row))
      },
      handleAdd(){
        this.form={}
        this.dialogVisible=true
      },
      save(){
        this.$refs.formRef.validate((validate)=>{
          if(validate){
            this.$request({
              url:this.form.id?'/user/update':'user/add',
              method:this.form.id? 'PUT':'POST',
              data:this.form
            }).then(res=>{
              if(res.code=='1'){
                this.$message.success('保存成功')
                localStorage.setItem('user',JSON.stringify(this.user))
                this.dialogVisible=false
              }
              else{
                this.$message.error(res.msg)
              }
            })
          }
        })
      },
      reset(){
        this.name=' '
        this.username=' '
        this.load()
      },
      load(pageNum){
        if(pageNum)this.pageNum=pageNum
        request.get('/user/selectByPage/admin',{
          params:{
            pageNum:this.pageNum,
            pageSize:this.pageSize,
            username:this.username,
            name:this.name
          }
        }).then(res=>{
          this.users=res.data.records
          this.total=res.data.total
        })
      },
       handleCurrentChange(pageNum){
        this.pageNum=pageNum
        this.load(pageNum)
       },
       handleSizeChange(pageSize){
        this.pageSize=pageSize;
        this.load()
       },
       handleAvatarSuccess(response,file,fileList) {
              console.log("1");
              console.log(response,file,fileList);
              this.form.image=response.data;
           },
  
     }
  };
  </script>
  <style lang='' scoped>
  </style>